namespace Todos.Application.TodoItems.Queries;

[AuthConfig(typeof(TodoItem), ResourceAccessType.RoleLimit, "Read")]
public class GetTodoItemsQuery : IRequest<List<TodoItemOutDto>>
{
    public long ListId { get; set; }
}

public class GetTodoItemsQueryHandler(IQueryRepository<TodoList> repository) : IRequestHandler<GetTodoItemsQuery, List<TodoItemOutDto>>
{
    public async Task<List<TodoItemOutDto>> Handle(GetTodoItemsQuery request, CancellationToken cancellationToken)
    {
        var spec = new TodoListByIdSpec(request.ListId).And(new TodoListIncludeItemsSpec());
        var list = await repository.GetAsync(spec, cancellationToken);
        
        if (list == null)
        {
            return new List<TodoItemOutDto>();
        }

        return list.Items.MapTo<List<TodoItemOutDto>>();
    }
}
